package ide.ccf.ccproject.preferences;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JOptionPane;

import ide.ccf.ccproject.CCFProject;
import ide.ccf.ccproject.ProjectPlugin;
import ide.ccf.ccproject.common.Utils;
import ide.ccf.ccproject.translate.Message;
import ide.ccf.ccproject.wizards.VersionWizard;
import ide.ccf.ccproject.wizards.pages.CCFMainWizardPage;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

/**
 * CCF版本改变监听器
 * 
 */
class VersionListener extends SelectionAdapter {
	public final static int ADD = 1;
	public final static int EDIT = 2;
	public final static int REMOVE = 3;
	public final static int DEFAULT = 4;
	public final static int SELECTION = 5;

	private CCFPreferencePage m_prefpage;
	private int m_control;

	/*
	 * //** 构造器
	 * 
	 * @param prefpage
	 * 
	 * @param control
	 */
	public VersionListener(CCFPreferencePage prefpage, int control) {
		m_control = control;
		m_prefpage = prefpage;
	}

	/*
	 * //** 选中事件
	 */
	public void widgetSelected(SelectionEvent e) {
		if (m_control == ADD) {
			VersionWizard versionWizard = new VersionWizard();
			WizardDialog versionDialog = new WizardDialog(
					m_prefpage.getShell(), versionWizard);
			versionDialog.create();
			versionDialog.setTitle("添加CCF版本");

			if (versionDialog.open() == WizardDialog.OK) {
				m_prefpage.addItem(versionWizard.getVersionName(),
						versionWizard.getBinPath(),
						versionWizard.getIncludePath());
			}
		} else if (m_control == EDIT) {
			String[] current = m_prefpage.getCurrentItem();
			VersionWizard versionWizard = new VersionWizard();
			WizardDialog versionDialog = new WizardDialog(
					m_prefpage.getShell(), versionWizard);

			versionDialog.create();

			versionWizard.setVersionName(current[0]);
			versionWizard.setBinPath(current[1]);
			versionWizard.setIncludePath(current[2]);
			versionDialog.setTitle("编辑CCF版本");

			if (versionDialog.open() == WizardDialog.OK) {
				m_prefpage.updateItem(versionWizard.getVersionName(),
						versionWizard.getBinPath(),
						versionWizard.getIncludePath());
			}
		} else if (m_control == REMOVE) {
			m_prefpage.removeItem();
		} else if (m_control == DEFAULT) {
			m_prefpage.setCurrentDefault();
		} else if (m_control == SELECTION) {
			Table table = (Table) e.widget;
			m_prefpage.enableButtons(table.getSelectionCount() > 0);
		}
	}
}

public class CCFPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage, SelectionListener {
	private Composite p;
	private Text t_ccf;
	private Combo c_sdk, c_runtime;
	protected static String strRuntime;
	protected static String strSdk;
	private Label label;
	private Table table;
	private Button removeButton;
	private Button editButton;
	private Button defaultButton;
	private Button autosetmkspec;
	private Button autosetmkcmd;

	public void init(IWorkbench workbench) {
		strRuntime = "";
		strSdk = "";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected Control createContents(Composite parent) {
		p = parent;
		Composite c = new Composite(parent, SWT.NULL);
		c.setLayoutData(new GridData(GridData.FILL_BOTH));
		c.setLayout(new GridLayout(1, true));

		Composite g_u = new Composite(c, SWT.NONE);
		GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd1.heightHint = 90;
		g_u.setLayoutData(gd1);
		g_u.setLayout(new GridLayout(3, false));
		{
			Label l = new Label(g_u, SWT.NULL);
			l.setText(Message.CCFPreferencdPage_0);
			t_ccf = new Text(g_u, SWT.BORDER | SWT.SINGLE);
			t_ccf.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
					| GridData.HORIZONTAL_ALIGN_FILL));
			Button bt = new Button(g_u, SWT.PUSH);
			bt.setText(Message.CCFPreferencdPage_1);
			bt.addSelectionListener(this);
		}

		Group g_d = new Group(c, SWT.NONE);
		GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd2.verticalSpan = 20;
		gd2.heightHint = 80;
		g_d.setLayoutData(gd2);
		g_d.setText(Message.CCProjectWizardOnePage_0);
		g_d.setLayout(new GridLayout(2, false));
		{
			Label l_1 = new Label(g_d, SWT.NONE);
			l_1.setText(Message.CCFPreferencdPage_2);
			l_1.setFont(g_d.getFont());
			l_1.setLayoutData(new GridData(GridData.BEGINNING));
			c_sdk = new Combo(g_d, SWT.DROP_DOWN | SWT.READ_ONLY);
			c_sdk.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			c_sdk.setFont(c.getFont());
			c_sdk.addSelectionListener(new SelectionAdapter() {
				//
			});
			Label l_2 = new Label(g_d, SWT.NONE);
			l_2.setText(Message.CCFPreferencdPage_3);
			l_2.setFont(g_d.getFont());
			l_2.setLayoutData(new GridData(GridData.BEGINNING));
			c_runtime = new Combo(g_d, SWT.DROP_DOWN | SWT.READ_ONLY);
			c_runtime.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			c_runtime.setFont(g_d.getFont());
		}
		Set setSdk = new HashSet();
		Set setRuntime = new HashSet();
		if (Utils.runCommand("which ccf", 1).equals("1")) {
			t_ccf.setText("ccf");
			String output = Utils.runCommand("ccf list --runtime -d", 0);
			String[] s = output.split("\r\n");
			// String[] s = {"ccf.gnome.runtime/aarch64/1.0",
			// "ccf.gnome.runtime/aarch64/2.0", "ccf.gnome.sdk/aarch64/1.0",
			// "ccf.gnome.sdk/aarch64/2.0"};
			for (int i = 0; i < s.length; i++) {
				String[] strtmp = s[i].split(" ");
				if (strtmp.length > 0) {
					String[] strTmp = strtmp[0].split("/");
					if (strTmp.length > 0) {
						String[] tmp = strTmp[0].split("\\.");
						if (tmp.length > 1) {
							if (tmp[2] != null && tmp[2].equals("runtime"))
								setRuntime.add(strtmp[0]);
							else if (tmp[2] != null && tmp[2].equals("sdk"))
								setSdk.add(strtmp[0]);
						}
					}

				}
			}
			String[] strSetSdk = new String[setSdk.size()];
			setSdk.toArray(strSetSdk);
			String[] strSetRuntime = new String[setRuntime.size()];
			setRuntime.toArray(strSetRuntime);
			c_runtime.setItems(strSetRuntime);
			c_sdk.setItems(strSetSdk);

			IPreferenceStore store = ProjectPlugin.getDefault()
					.getPreferenceStore();
			store.setValue(PreferenceConstants.CCFRUNTIME_VERSIONS,
					setRuntime.toString());
			store.setValue(PreferenceConstants.CCFSDK_VERSIONS,
					setSdk.toString());

			for (int j = 0; j < setSdk.size(); j++)
				c_runtime.setData("" + j, strSetSdk[j]);
			for (int k = 0; k < setRuntime.size(); k++)
				c_sdk.setData("" + k, strSetRuntime[k]);

			// get the versions of ccf
			output = Utils.runCommand("ccf --version", 0);
			s = output.split("\r\n");
			Set setCCF = new HashSet();
			for (int k = 0; k < s.length; k++)
				setCCF.add(s[k]);
			store.setValue(PreferenceConstants.CCF_VERSIONS, setCCF.toString());

		}
		c_sdk.setText(getCCFSdkVersion() == null ? "" : getCCFSdkVersion());
		c_runtime.setText(getCCFRuntimeVersion() == null ? ""
				: getCCFRuntimeVersion());

		Composite composite = new Composite(c, SWT.NULL);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		addBuildsSection(composite);
		updateItems();
		enableButtons(table.getSelectionCount() > 0);

		return c;
	}

	public static String getCCFRuntimeVersion() {
		if (ProjectPlugin.getDefault() != null) {
			IPreferenceStore store = ProjectPlugin.getDefault()
					.getPreferenceStore();

			if (!store.contains(PreferenceConstants.CCFRUNTIME_VERSION))
				return null;

			String defaultVersion = store
					.getString(PreferenceConstants.CCFRUNTIME_VERSION);
			// cut string
			String[] b = defaultVersion.split(" ");
			if (b.length > 0)
				return b[0].toString();
			return defaultVersion;
		}
		return null;

	}

	public static String getCCFSdkVersion() {
		if (ProjectPlugin.getDefault() != null) {
			IPreferenceStore store = ProjectPlugin.getDefault()
					.getPreferenceStore();

			if (!store.contains(PreferenceConstants.CCFSDK_VERSION))
				return null;

			String defaultVersion = store
					.getString(PreferenceConstants.CCFSDK_VERSION);
			// cut string
			String[] b = defaultVersion.split(" ");
			if (b.length > 0)
				return b[0].toString();
			return defaultVersion;
		}
		return null;

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean performOk() {
		IPreferenceStore store = ProjectPlugin.getDefault()
				.getPreferenceStore();

		// 获取所有项目CCF路径
		IProject[] pros = Utils.getProjects();
		CCFProject[] projects = new CCFProject[pros.length];
		String[] oldBinPaths = new String[pros.length];
		String[] oldIncludePaths = new String[pros.length];

		for (int i = 0; i < pros.length; ++i) {
			projects[i] = new CCFProject(pros[i]);
			oldBinPaths[i] = projects[i].getBinPath();
			oldIncludePaths[i] = projects[i].getIncludePath();
		}
		strRuntime = c_runtime.getText();
		strSdk = c_sdk.getText();
		if (strRuntime != null)
			store.setValue(PreferenceConstants.CCFRUNTIME_VERSION, strRuntime);
		if (strSdk != null)
			store.setValue(PreferenceConstants.CCFSDK_VERSION, strSdk);

		String defaultVersion = getDefaultVersionName();

		if (defaultVersion != null)
			store.setValue(PreferenceConstants.CCFVERSION_DEFAULT,
					defaultVersion);

		store.setValue(PreferenceConstants.CCFVERSION_COUNT,
				table.getItemCount());

		for (int i = 0; i < table.getItemCount(); ++i) {
			store.setValue(
					PreferenceConstants.CCFVERSION_NAME + "."
							+ Integer.toString(i), table.getItem(i).getText(0));
			store.setValue(PreferenceConstants.CCFVERSION_BINPATH + "."
					+ Integer.toString(i), table.getItem(i).getText(1));
			store.setValue(PreferenceConstants.CCFVERSION_INCLUDEPATH + "."
					+ Integer.toString(i), table.getItem(i).getText(2));
		}

		store.setValue(PreferenceConstants.AUTOSETMKSPEC,
				autosetmkspec.getSelection());
		store.setValue(PreferenceConstants.AUTOSETMKCMD,
				autosetmkspec.getSelection());

		Vector outdated = new Vector();

		// 更新所有CCF项目并且收集所有需要重新构建的项目
		for (int i = 0; i < projects.length; ++i) {
			projects[i].updateDir(oldBinPaths[i], oldIncludePaths[i]);
			if ((projects[i].getBinPath() == null && oldBinPaths[i] != null)
					|| (projects[i].getBinPath() != null && !projects[i]
							.getBinPath().equals(oldBinPaths[i]))
					|| (projects[i].getIncludePath() == null && oldIncludePaths[i] != null)
					|| (projects[i].getIncludePath() != null && !projects[i]
							.getIncludePath().equals(oldIncludePaths[i]))) {
				outdated.add(projects[i]);
			}
		}

		if (!outdated.isEmpty())
			askForRebuild(outdated);

		return true;
	}

	public CCFPreferencePage() {

	}

	public static boolean getAutoSetMkSpec() {
		IPreferenceStore store = ProjectPlugin.getDefault()
				.getPreferenceStore();

		if (store.contains(PreferenceConstants.AUTOSETMKSPEC))
			return store.getBoolean(PreferenceConstants.AUTOSETMKSPEC);
		else
			return true;
	}

	public static boolean getAutoSetMkCmd() {
		IPreferenceStore store = ProjectPlugin.getDefault()
				.getPreferenceStore();

		if (store.contains(PreferenceConstants.AUTOSETMKCMD))
			return store.getBoolean(PreferenceConstants.AUTOSETMKCMD);
		else
			return true;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String[] getCCFVersions() {
		Vector versions = new Vector();
		IPreferenceStore store = ProjectPlugin.getDefault()
				.getPreferenceStore();

		if (!store.contains(PreferenceConstants.CCFVERSION_COUNT))
			return null;

		int count = store.getInt(PreferenceConstants.CCFVERSION_COUNT);

		for (int i = 0; i < count; ++i) {
			String name = PreferenceConstants.CCFVERSION_NAME + "."
					+ Integer.toString(i);

			if (store.contains(name))
				versions.add(store.getString(name));
		}
		return (String[]) versions.toArray(new String[versions.size()]);
	}

	private static String[] getCCFPaths(String version) {
		IPreferenceStore store = ProjectPlugin.getDefault()
				.getPreferenceStore();

		if (!store.contains(PreferenceConstants.CCFVERSION_COUNT))
			return null;

		String defaultVersionName = store
				.getString(PreferenceConstants.CCFVERSION_DEFAULT);
		String defaultVersionBinPath = "";
		String defaultVersionIncludePath = "";
		int count = store.getInt(PreferenceConstants.CCFVERSION_COUNT);

		for (int i = 0; i < count; ++i) {
			String nameKey = PreferenceConstants.CCFVERSION_NAME + "."
					+ Integer.toString(i);
			String binpathKey = PreferenceConstants.CCFVERSION_BINPATH + "."
					+ Integer.toString(i);
			String includepathKey = PreferenceConstants.CCFVERSION_INCLUDEPATH
					+ "." + Integer.toString(i);
			String name = "";
			String binpath = "";
			String includepath = "";

			if (store.contains(nameKey))
				name = store.getString(nameKey);

			if (store.contains(binpathKey))
				binpath = store.getString(binpathKey);

			if (store.contains(includepathKey))
				includepath = store.getString(includepathKey);

			if (name.equals(version))
				return new String[] { name, binpath, includepath };

			if (name.equals(defaultVersionName)) {
				defaultVersionBinPath = binpath;
				defaultVersionIncludePath = includepath;
			}
		}
		return new String[] { defaultVersionName, defaultVersionBinPath,
				defaultVersionIncludePath };
	}

	public static String getCCFVersionBinPath(String version) {
		String[] paths = getCCFPaths(version);

		if (paths != null)
			return getCCFPaths(version)[1];

		return null;
	}

	public static String getCCFVersionIncludePath(String version) {
		String[] paths = getCCFPaths(version);

		if (paths != null)
			return getCCFPaths(version)[2];

		return null;
	}

	public String[] getCurrentItem() {
		TableItem[] items = table.getSelection();

		if (items.length == 0)
			return null;

		return new String[] { items[0].getText(0), items[0].getText(1),
				items[0].getText(2) };
	}

	public void enableButtons(boolean enabled) {
		removeButton.setEnabled(enabled);
		defaultButton.setEnabled(enabled);
		editButton.setEnabled(enabled);
	}

	public void updateItem(String name, String binPath, String includePath) {
		TableItem[] items = table.getSelection();

		if (items.length == 0)
			return;

		items[0].setText(new String[] { name, binPath, includePath });
	}

	public void addItem(String name, String binPath, String includePath) {
		TableItem item = new TableItem(table, SWT.NONE);
		item.setText(new String[] { name, binPath, includePath });
	}

	public void removeItem() {
		if (table.getSelectionCount() > 0) {
			int removeindex = table.getSelectionIndex();
			int defindex = getDefaultIndex();

			table.remove(removeindex);

			if (removeindex == defindex)
				setDefaultIndex(0);
		}
		enableButtons(table.getSelectionCount() > 0);
	}

	@SuppressWarnings("rawtypes")
	private void askForRebuild(final Vector projects) {
		Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
		MessageDialog dialog = new MessageDialog(shell, "CCF版本变化", null,
				"某些项目的CCF版本设置已经发生改变。需要重建构建项目以使设置生效。是否现在重新构建？",
				MessageDialog.QUESTION,
				new String[] { IDialogConstants.YES_LABEL,
						IDialogConstants.NO_LABEL }, 0);

		if (dialog.open() == 0) {
			WorkspaceJob rebuild = new WorkspaceJob("重新构建项目") {
				public boolean belongsTo(Object family) {
					return ResourcesPlugin.FAMILY_MANUAL_BUILD.equals(family);
				}

				public IStatus runInWorkspace(IProgressMonitor monitor) {
					Iterator i = projects.iterator();

					while (i.hasNext()) {
						CCFProject project = (CCFProject) i.next();
						project.scheduleRebuild();
					}

					return Status.OK_STATUS;
				}
			};
			rebuild.setRule(ResourcesPlugin.getWorkspace().getRuleFactory()
					.buildRule());
			rebuild.setUser(true);
			rebuild.schedule();
		}
	}

	private void setDefaultIndex(int index) {
		for (int i = 0; i < table.getItemCount(); ++i) {
			TableItem item = table.getItem(i);
			Font fnt = item.getFont();
			FontData fntdata = fnt.getFontData()[0];

			if (i == index) {
				int style = fntdata.getStyle() | SWT.BOLD;
				fntdata.setStyle(style);
				item.setFont(new Font(fnt.getDevice(), fntdata));
			} else if ((fntdata.getStyle() & SWT.BOLD) != 0) {
				int style = fntdata.getStyle() & ~SWT.BOLD;
				fntdata.setStyle(style);
				item.setFont(new Font(fnt.getDevice(), fntdata));
			}
		}
	}

	private int getDefaultIndex() {
		for (int i = 0; i < table.getItemCount(); ++i) {
			TableItem item = table.getItem(i);
			Font fnt = item.getFont();
			FontData fntdata = fnt.getFontData()[0];

			if ((fntdata.getStyle() & SWT.BOLD) != 0)
				return i;
		}

		if (table.getItemCount() > 0)
			return 0;

		return -1;
	}

	private String getDefaultVersionName() {
		int index = getDefaultIndex();

		if (index == -1)
			return null;

		return table.getItem(index).getText(0);
	}

	public void setCurrentDefault() {
		setDefaultIndex(table.getSelectionIndex());
	}

	private void updateItems() {
		if (ProjectPlugin.getDefault() == null) {
			System.out.println("come here!");
		}
		IPreferenceStore store = ProjectPlugin.getDefault()
				.getPreferenceStore();

		autosetmkspec.setSelection(getAutoSetMkSpec());
		autosetmkcmd.setSelection(getAutoSetMkCmd());

		if (!store.contains(PreferenceConstants.CCFVERSION_COUNT))
			return;

		int defaultVersionIndex = 0;
		String defaultVersionName = store
				.getString(PreferenceConstants.CCFVERSION_DEFAULT);
		int count = store.getInt(PreferenceConstants.CCFVERSION_COUNT);

		for (int i = 0; i < count; ++i) {
			String nameKey = PreferenceConstants.CCFVERSION_NAME + "."
					+ Integer.toString(i);
			String binpathKey = PreferenceConstants.CCFVERSION_BINPATH + "."
					+ Integer.toString(i);
			String includepathKey = PreferenceConstants.CCFVERSION_INCLUDEPATH
					+ "." + Integer.toString(i);
			String name = "";
			String binpath = "";
			String includepath = "";

			if (store.contains(nameKey))
				name = store.getString(nameKey);

			if (store.contains(binpathKey))
				binpath = store.getString(binpathKey);

			if (store.contains(includepathKey))
				includepath = store.getString(includepathKey);

			addItem(name, binpath, includepath);

			if (name.equals(defaultVersionName))
				defaultVersionIndex = i;
		}
		setDefaultIndex(defaultVersionIndex);
	}

	private void addBuildsSection(Composite parent) {
		label = new Label(parent, SWT.CENTER);
		label.setText("CCF版本列表:");

		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
		gridData.horizontalSpan = 2;
		label.setLayoutData(gridData);

		table = new Table(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		TableColumn column;
		column = new TableColumn(table, SWT.LEFT);
		column.setText("名称");
		column.setWidth(100);
		column.setResizable(true);
		column = new TableColumn(table, SWT.LEFT);
		column.setText("Bin路径");
		column.setResizable(true);
		column.setWidth(100);
		column = new TableColumn(table, SWT.LEFT);
		column.setText("Include路径");
		column.setResizable(true);
		column.setWidth(100);

		gridData = new GridData();
		gridData.verticalSpan = 5;
		gridData.widthHint = 250;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;

		table.setLayoutData(gridData);
		table.addSelectionListener(new VersionListener(this,
				VersionListener.SELECTION));

		Button addButton = new Button(parent, SWT.NONE);
		addButton.addSelectionListener(new VersionListener(this,
				VersionListener.ADD));
		addButton.setText("添加...");
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		addButton.setLayoutData(gridData);

		editButton = new Button(parent, SWT.NONE);
		editButton.addSelectionListener(new VersionListener(this,
				VersionListener.EDIT));
		editButton.setText("编辑...");
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		editButton.setLayoutData(gridData);

		removeButton = new Button(parent, SWT.NONE);
		removeButton.addSelectionListener(new VersionListener(this,
				VersionListener.REMOVE));
		removeButton.setText("删除");
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		removeButton.setLayoutData(gridData);

		defaultButton = new Button(parent, SWT.NONE);
		defaultButton.addSelectionListener(new VersionListener(this,
				VersionListener.DEFAULT));
		defaultButton.setText("设为默认");
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		defaultButton.setLayoutData(gridData);

		Composite spacer = new Composite(parent, SWT.NONE);
		gridData = new GridData();
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalAlignment = GridData.FILL;
		spacer.setLayoutData(gridData);

		autosetmkspec = new Button(parent, SWT.CHECK);
		autosetmkspec.setText("应用修改时自动更新QMAKESPEC。");
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		autosetmkspec.setLayoutData(gridData);

		autosetmkcmd = new Button(parent, SWT.CHECK);
		autosetmkcmd.setText("应用修改时自动更新make命令。");
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		autosetmkcmd.setLayoutData(gridData);
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void widgetSelected(SelectionEvent e) {
		Shell shell = p.getShell();
		DirectoryDialog dlg = new DirectoryDialog(shell);
		dlg.setFilterPath("cccc");
		dlg.setText(Message.CCFPreferencdPage_4);
		dlg.setMessage(Message.CCFPreferencdPage_5);
		String dir = dlg.open();
		if (dir != null) {
			t_ccf.setText(dir);
			String ds = dir + "/ccf";
			if (Utils.isExistFile(dir + "/ccf")) {
				Set setSdk = new HashSet();
				Set setRuntime = new HashSet();
				String output = Utils.runCommand(ds + "  list --runtime -d", 0);
				// System.out.println("output:"+output);
				String[] s = output.split("\r\n");
				// String[] s = {"ccf.gnome.runtime/aarch64/1.0",
				// "ccf.gnome.runtime/aarch64/2.0", "ccf.gnome.sdk/aarch64/1.0",
				// "ccf.gnome.sdk/aarch64/2.0"};
				if (s.length <= 1)
					JOptionPane.showMessageDialog(null, output);
				else {
					for (int i = 0; i < s.length; i++) {
						String[] strtmp = s[i].split(" ");
						if (strtmp.length > 0) {
							String[] strTmp = strtmp[0].split("/");
							if (strTmp.length > 0) {
								String[] tmp = strTmp[0].split("\\.");
								if (tmp.length > 0) {
									if (tmp[2] != null
											&& tmp[2].equals("runtime"))
										setRuntime.add(strtmp[0]);
									else if (tmp[2] != null
											&& tmp[2].equals("sdk"))
										setSdk.add(strtmp[0]);
								}
							}
						}
					}
					String[] strSetSdk = new String[setSdk.size()];
					setSdk.toArray(strSetSdk);
					String[] strSetRuntime = new String[setRuntime.size()];
					setRuntime.toArray(strSetRuntime);
					c_runtime.setItems(strSetRuntime);
					c_sdk.setItems(strSetSdk);
					if (strSetRuntime[0] != null)
						c_runtime.setText(strSetRuntime[0]);
					if (strSetSdk[0] != null)
						c_sdk.setText(strSetSdk[0]);

					IPreferenceStore store = ProjectPlugin.getDefault()
							.getPreferenceStore();
					store.setValue(PreferenceConstants.CCFRUNTIME_VERSIONS,
							setRuntime.toString());
					store.setValue(PreferenceConstants.CCFSDK_VERSIONS,
							setSdk.toString());

					for (int j = 0; j < setSdk.size(); j++)
						c_runtime.setData("" + j, strSetSdk[j]);
					for (int k = 0; k < setRuntime.size(); k++)
						c_sdk.setData("" + k, strSetRuntime[k]);
				}
			}
		}
	}
}